package com.cn.action;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.cn.bean.Provider;
import com.cn.bean.Provider_servicecatalog;
import com.cn.service.AdminService;
import com.cn.service.ProviderService;
import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ProviderAction extends ActionSupport{

	private Provider provider;
	private File permit;
	private String permitFileName;
	private String permitContentType;
	private InputStream inputStream;
	private String servicecatalog;
	private ProviderService providerService = null;
	private AdminService adminService =null;
	public AdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

	private String result;


	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	HttpServletRequest request;
	HttpServletResponse response;

	public String getServicecatalog() {
		return servicecatalog;
	}

	public void setServicecatalog(String servicecatalog) {
		this.servicecatalog = servicecatalog;
	}

	public File getPermit() {
		return permit;
	}

	public void setPermit(File permit) {
		this.permit = permit;
	}

	public String getPermitFileName() {
		return permitFileName;
	}

	public void setPermitFileName(String permitFileName) {
		this.permitFileName = permitFileName;
	}

	public String getPermitContentType() {
		return permitContentType;
	}

	public void setPermitContentType(String permitContentType) {
		this.permitContentType = permitContentType;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public Provider getProvider() {
		return provider;
	}

	public void setProvider(Provider provider) {
		this.provider = provider;
	}

	public ProviderService getProviderService() {
		return providerService;
	}

	public void setProviderService(ProviderService providerService) {
		this.providerService = providerService;
	}

	/**
	 * 获取ST供应商 输出到安卓端
	 */
	public void getSTProviders() throws Exception {
		List list = providerService.getSTProviders();
		Gson gson = new Gson();
		String json = gson.toJson(list);
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		response.setContentType("text/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.write(json);
	}

	/**
	 * 获取SC供应商 输出到安卓端
	 */
	public void getSCProviders() throws Exception {
		List list = providerService.getSCProviders();
		Gson gson = new Gson();
		String json = gson.toJson(list);
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		response.setContentType("text/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.write(json);
	}
/**
 * 返回给安卓端金牌供应商列表
 * @throws Exception
 */
	public void getRecommendProviders() throws Exception {
		List list = providerService.getRecommendProviders();
		Gson gson = new Gson();
		String json = gson.toJson(list);
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		response.setContentType("text/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.write(json);
	}
	
	
	
	
	/**
	 * 供应商注册 并保存许可证的图片到服务器
	 * 
	 * @return 注册结果
	 */
	public String providerRegister() {
		try {
			String realpath = ServletActionContext.getServletContext().getRealPath("/permit");
			System.out.println("上传到服务器的地址realpath: " + realpath);
			String path = realpath + "\\" + getPermitFileName();
			System.out.println("具体的地址路径" + path);
			System.out.println(getPermitContentType());
			String prefix = getPermitFileName().substring(getPermitFileName().lastIndexOf(".") + 1);
			if (permit != null) {
				File savefile = new File(new File(realpath),  provider.getUsername()+"."+prefix);
				if (!savefile.getParentFile().exists())
					savefile.getParentFile().mkdirs();
				FileUtils.copyFile(permit, savefile);
				ActionContext.getContext().put("message", "文件上传成功");
				provider.setPermitpath("permit/" + provider.getUsername()+"."+prefix);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (providerService.providerRegister(provider)) {
			return "registersuccess";
		} else {
			return "registerfail";
		}

	}

	/**
	 * 供应商登录
	 * 
	 * @return 登录结果
	 */
	public String providerLogin() {
		if(provider.getUsername().equals("admin")&&provider.getPassword().equals("123")){
			ActionContext ctx = ActionContext.getContext();
			Map<String, Object> session;
			session = (Map<String, Object>) ctx.getSession();
			session.put("username", "admin");
			return "admin";
		}
		if (providerService.providerLogin(provider))
			return "loginsuccess";
	
		else{
			return "loginfail";
		}

	}

	// 供应商选择服务种类
	public String providerSelectCatalog() {
		String result = "success";

		String catalogid[] = servicecatalog.split(",");// 截取字符串，获得各个checkBox的值
		for (int i = 0; i < catalogid.length; i++) {
			System.out.print(catalogid[i].trim());
			// 并存入数据库
			int sc_id = Integer.parseInt(catalogid[i].trim());

			Map session = (Map) ActionContext.getContext().get("session");
			provider = (Provider) session.get("provider");
			List list=new ArrayList();
			list=providerService.showServicePrice(provider.getId());
			if(list.get(0)!=null)
				session.put("priceList", list);


			if (providerService.providerSaveCatalog(provider.getId(), sc_id) == false) {

				result = "fail";
				return result;
			}
		}
		return result;
	}

	// 得到供应商的未接订单
	public String getWaitTakeOrderByProvider() {
		Map session = (Map) ActionContext.getContext().get("session");
		provider = (Provider) session.get("provider");
		System.out.println(provider.getId());
		List list = providerService.getWaitTakeOrderByProvider(provider.getId());
		System.out.println(list.size());
		session.put("WaitTakeOrderList", list);
		return "success";

	}

	// 得到供应商的所有订单
	public String getAllOrderByProvider() {
		Map session = (Map) ActionContext.getContext().get("session");
		provider = (Provider) session.get("provider");
		System.out.println(provider.getId());
		System.out.println(provider.getUsername());
		System.out.println(provider.getRank());
		List list = providerService.getAllOrderByProvider(provider.getId());
		System.out.println(list.size());
		session.put("AllOrderList", list);
		return "success";

	}

	/**
	 * 得到所有未认证供应商
	 * 
	 * @return 成功
	 */
	public String listAllUNAUTHORIZED() {
		Map session = (Map) ActionContext.getContext().get("session");
		List list = providerService.listAllUNAUTHORIZED();
		session.put("UNAUTHORIZEDList", list);
		return "success";
	}

	/**
	 * 得到供应商详情
	 * 
	 * @return
	 */
	public String providerDetail() {
		int pro_id = provider.getId();
		providerService.providerDetail(pro_id);
		return "success";

	}
	
	//供应商修改服务价格
	public String showServicePrice()
	{
		Map session = (Map) ActionContext.getContext().get("session");
		provider = (Provider) session.get("provider");
		List list=new ArrayList();
		list=providerService.showServicePrice(provider.getId());
		if(list.get(0)!=null)
		{
			session.put("priceList", list);	
			System.out.println("已将priceList放入session");
			return "success";
		}
		else 
			return "fail";
	}
	//检查重复的供应商用户名
	/**
	 * 检查供应商的username是否存在
	 */
	public void checkProviderUsername()
	{
		this.setResult(providerService.checkProviderUsername(provider.getUsername()));
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter writer = null;
		try {
			response.setContentType("text/html;charset=UTF-8");
			writer = response.getWriter();
			writer.write(result);
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
	//取消服务供应商
	public String banProvider()
	{
		int pro_id=provider.getId();
		System.out.println("要删除的供应商是"+pro_id);
		return adminService.banProvider(pro_id);
	}
	//检查重复的供应商服务类型
	public void checkProviderCatalog()
	{
		this.setResult("false");
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter writer = null;
		try {
			response.setContentType("text/html;charset=UTF-8");
			writer = response.getWriter();
			writer.write(result);
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	
	}
	
	/**
	 * 每周自动更新供应商的评分
	 */
	public void providerRank(){
		System.out.println("正在更新供应商的评分");
		providerService.providerRank();
	}
	
	//列出所有被收藏的供应商
	public void listCollectedProvider(){
		try {
		List list = providerService.listCollectedProvider();
		Gson gson = new Gson();
		String json = gson.toJson(list);
		response = ServletActionContext.getResponse();
		response.setContentType("text/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.write(json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 注销
	 * @return
	 */
	public String logout(){
		providerService.logout();
		return "success";
	}
}
